package com.example.baozidemo.commons.mybatis;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;

/**
 * 自动注入处理类
 *
 * @author steed
 * @since 2020/11/11
 **/
@Component
public class FieldMetaObjectHandler implements MetaObjectHandler {

    private final static String CREATE_DATE = "createDate";
    private final static String UPDATE_DATE = "updateDate";

    @Override
    public void insertFill(MetaObject metaObject) {
        // 创建时间
        setFieldValByName(CREATE_DATE, LocalDateTime.now(), metaObject);
        // 更新时间
        setFieldValByName(UPDATE_DATE, LocalDateTime.now(), metaObject);
    }

    @Override
    public void updateFill(MetaObject metaObject) {
        // 更新时间
        setFieldValByName(UPDATE_DATE, LocalDateTime.now(), metaObject);
    }
}
